package switchviz.core.alias;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.tree.TreePath;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import switchviz.core.ResourceManager;
import switchviz.core.mib.MIBTree;
import switchviz.gui.component.XMLTree;
import switchviz.gui.component.XMLTreeNode;

public class AliasTree extends XMLTree
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1558862673353930919L;
	public static final String ALIASNODENAMEPATH = "namePath";
	public static final String ALIASNODEOIDPATH = "oidPath";
	
	public AliasTree(File xmlfile) throws JDOMException, IOException 
	{
		super(xmlfile);
		this.getTree().setRootVisible(false);
	    this.setLeafNodeIcon(ResourceManager.getInstance().getImageIconAlias());
	}
	
	public static String parseAlias(String alias) throws JDOMException, IOException
	{
		SAXBuilder parser = new SAXBuilder();
		Document doc = parser.build(ResourceManager.getInstance().getAliasConfigurationFile());
		return doc.getRootElement().getChild(alias).getAttributeValue(ALIASNODEOIDPATH);
	}
	
	public static List<AliasDataModel> getAliasList() throws JDOMException, IOException
	{
		ArrayList<AliasDataModel> aliaslist = new ArrayList<AliasDataModel>();
		SAXBuilder parser = new SAXBuilder();
		Document doc = parser.build(ResourceManager.getInstance().getAliasConfigurationFile());
		Iterator<?> it = doc.getRootElement().getChildren().iterator();
		while(it.hasNext())
		{
			Element anode = (Element)it.next();
			aliaslist.add(new AliasDataModel(anode.getName(), anode.getAttributeValue(ALIASNODENAMEPATH),
					anode.getAttributeValue(ALIASNODEOIDPATH), anode.getTextNormalize()));
		}
		return aliaslist;
	}
	
	public void transferSelection(TreePath[] Selection)
	{
		XMLTreeNode currentNode = null;
		XMLTreeNode newNode = null;
		XMLTreeNode tempNode = null;
		Element newElement = null;
		String name = "", oid = "";
		int j, i, k;
		
		for(i = 0; i < Selection.length; ++i)
		{
			currentNode = (XMLTreeNode) Selection[i].getLastPathComponent();
			for(k = 0; k < currentNode.getChildCount(); ++k)
			{
				TreePath[] tempPath = new TreePath[1];
				tempPath[0] = new TreePath(((XMLTreeNode)currentNode.getChildAt(k)).getPath());
				transferSelection(tempPath);
			}
			if(currentNode.getChildCount() > 0) { continue; }
			newElement = new Element(currentNode.getElement().getAttributeValue(MIBTree.MIBNODENAME));
			newElement.setText(currentNode.getElement().getChildTextNormalize(MIBTree.MIBNODEDESCRIPTION));
	    	Object[] pathlist = Selection[i].getPath();
	    	for(j = 1; j < pathlist.length; ++j)
	    	{
	    		tempNode = (XMLTreeNode) pathlist[j];
	    		name += tempNode.getElement().getAttributeValue(MIBTree.MIBNODENAME) + ".";
	    		oid += tempNode.getElement().getAttributeValue(MIBTree.MIBNODEOID) + ".";
	    	}
	    	if(j > 1)
	    	{
	        	name = name.substring(0, name.length() - 1);
	        	oid = oid.substring(0, oid.length() - 1);
	        	newElement.setAttribute(ALIASNODENAMEPATH, name);
				newElement.setAttribute(ALIASNODEOIDPATH, oid);
	    	}
			newNode = new XMLTreeNode(currentNode.getElement().getAttributeValue(MIBTree.MIBNODENAME), newElement);
			this.addNode(newNode, (XMLTreeNode)_treemodel.getRoot());
		}
	}
	
	public void changeCurrentNode(AliasDataModel datamodel)
	{
		XMLTreeNode actNode = (XMLTreeNode)_jtree.getLastSelectedPathComponent();	
		actNode.getElement().setAttribute(ALIASNODENAMEPATH, datamodel.getNamePath());
		actNode.getElement().setAttribute(ALIASNODEOIDPATH, datamodel.getOIDPath());
		actNode.getElement().setName(datamodel.getAlias());
		actNode.getElement().setText(datamodel.getDescription());
		actNode.setUserObject(datamodel.getAlias());
		_treemodel.reload(actNode);
	}
	
	public void deleteSelection(TreePath[] Selection)
	{
		XMLTreeNode currentNode = null;
		
		for(int i = 0; i < Selection.length; ++i)
		{
			currentNode = (XMLTreeNode) Selection[i].getLastPathComponent();
			if(currentNode.isRoot()) { continue; }
			this.removeNode(currentNode);
		}
	}

}
